package com.christina.engine.state.impl;

import com.christina.engine.state.AbstractConversationState;
import com.christina.engine.state.ConversationState;
import com.christina.engine.state.model.StateContext;
import com.christina.engine.state.model.StateTransitionResult;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;

/**
 * 处理状态
 * 负责处理用户请求并生成响应
 * 
 * @author Christina
 */
@Component
public class ProcessingState extends AbstractConversationState {
    
    public static final String STATE_NAME = "Processing";
    
    public ProcessingState() {
        super(STATE_NAME, StateType.PROCESSING, 60000); // 1分钟超时
    }
    
    @Override
    protected StateTransitionResult doOnEnter(StateContext context) {
        // 开始处理用户请求
        context.setStateData("processingStarted", true);
        context.setStateData("processingStep", "analyzing");
        
        return StateTransitionResult.stayInCurrentState("正在处理您的请求，请稍候...");
    }
    
    @Override
    protected StateTransitionResult doHandleMessage(String message, StateContext context) {
        // 在处理状态中，通常不接受新的用户输入
        // 但可以处理取消命令
        if (isCancelCommand(message)) {
            return StateTransitionResult.success(
                    getCompletedState(), 
                    "已取消处理。还有其他需要帮助的吗？"
            );
        }
        
        // 模拟处理过程
        String processingStep = context.getStateData("processingStep", "analyzing");
        
        switch (processingStep) {
            case "analyzing":
                context.setStateData("processingStep", "executing");
                return StateTransitionResult.stayInCurrentState("正在分析您的请求...");
                
            case "executing":
                context.setStateData("processingStep", "completing");
                return StateTransitionResult.stayInCurrentState("正在执行处理...");
                
            case "completing":
                // 处理完成，转到完成状态
                String result = generateProcessingResult(context);
                return StateTransitionResult.success(
                        getCompletedState(), 
                        result
                );
                
            default:
                return StateTransitionResult.failure("Unknown processing step: " + processingStep);
        }
    }
    
    @Override
    protected StateTransitionResult doOnTimeout(StateContext context) {
        return StateTransitionResult.success(
                getCompletedState(),
                "处理超时。请稍后重试或联系技术支持。"
        );
    }
    
    @Override
    public List<ConversationState> getPossibleNextStates(StateContext context) {
        return Arrays.asList(
                getCompletedState(),
                getInitialState()
        );
    }
    
    /**
     * 生成处理结果
     * 
     * @param context 状态上下文
     * @return 处理结果消息
     */
    private String generateProcessingResult(StateContext context) {
        String targetService = context.getStateData("targetService", "chat");
        String userMessage = context.getCurrentMessage();
        
        // 根据目标服务生成不同的结果
        switch (targetService) {
            case "chat":
                return "根据您的问题，我的回答是：这是一个示例回答。还有其他问题吗？";
                
            case "travel":
                return "已为您生成旅行规划建议。您可以查看详细的行程安排。需要进一步调整吗？";
                
            case "user":
                return "用户设置已更新完成。还需要修改其他设置吗？";
                
            default:
                return "处理完成。还有其他需要帮助的吗？";
        }
    }
    
    private ConversationState getCompletedState() {
        return new CompletedState();
    }
    
    private ConversationState getInitialState() {
        return new InitialState();
    }
}